package com.zheinfo.web.controller.biz;

import com.zheinfo.biz.domain.UserAddress;
import com.zheinfo.biz.service.IUserAddressService;
import com.zheinfo.common.core.controller.BaseController;
import com.zheinfo.common.core.domain.AjaxResult;
import com.zheinfo.common.core.page.TableDataInfo;
import com.zheinfo.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户收货地址Controller
 * 
 * @author zheinfo
 */
@RestController
@RequestMapping("/user/address")
public class UserAddressController extends BaseController
{
    @Autowired
    private IUserAddressService userAddressService;

    /**
     * 查询用户收货地址列表
     */
    @GetMapping("/list")
    public TableDataInfo list(UserAddress userAddress)
    {
        // 设置当前登录用户ID
        userAddress.setUserId(SecurityUtils.getUserId());
        startPage();
        List<UserAddress> list = userAddressService.selectUserAddressList(userAddress);
        return getDataTable(list);
    }

    /**
     * 获取用户收货地址详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userAddressService.selectUserAddressById(id));
    }

    /**
     * 新增用户收货地址
     */
    @PostMapping
    public AjaxResult add(@RequestBody UserAddress userAddress)
    {
        // 设置当前登录用户ID
        userAddress.setUserId(SecurityUtils.getUserId());
        return toAjax(userAddressService.insertUserAddress(userAddress));
    }

    /**
     * 修改用户收货地址
     */
    @PutMapping
    public AjaxResult edit(@RequestBody UserAddress userAddress)
    {
        return toAjax(userAddressService.updateUserAddress(userAddress));
    }

    /**
     * 删除用户收货地址
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userAddressService.deleteUserAddressByIds(ids));
    }
}